package com.linkage.im.utils;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.linkage.im.model.UserVo;
import com.linkage.im.processor.LoginServiceProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

public class UserInterfUtils {

    private final static Logger logger = LoggerFactory.getLogger(LoginServiceProcessor.class);

    private static String url="https://jbld.tiandao.fun:11001/exter";

    public static Map<String,String> login(String loginname, String password,String flag){
        String url = "";
        if(StrUtil.equals("0",flag)){
            url = "https://jbld.tiandao.fun:11001/exter/login";
        }else {
            url = "https://jbld.tiandao.fun:11001/exter/manage/login";
        }
        Map buss = MapUtil.builder().put("userMobile", loginname).put("userPassword", password).build();
        Map param = MapUtil.builder().put("buss", buss).put("base", null).build();
        HttpResponse response = HttpUtil.createPost(url).body(JSON.toJSONString(param), "application/json").execute();
        logger.info("调用登录接口返参：" + response.body());
        Map map = JSONObject.parseObject(response.body(), Map.class);
        Map returnBuss = JSONObject.parseObject(map.get("buss").toString(), Map.class);
        return returnBuss;
    }

    public static UserVo getUserInfo(String mobile, String  token){
        Map buss = MapUtil.builder().put("paramVar", mobile).build();
        Map base = MapUtil.builder().put("usertoken", token).build();
        Map param = MapUtil.builder().put("buss", buss).put("base", base).build();
        logger.info("访问url为："+url);
        HttpResponse response = HttpUtil.createPost(url+"/manage/getUserMgInfoVO").body(JSON.toJSONString(param), "application/json").execute();
        logger.info("调用查询用户接口返参：" + response.body());
        Map map = JSONObject.parseObject(response.body(), Map.class);
        UserVo user = JSONObject.parseObject(map.get("buss").toString(),UserVo.class);
        logger.info("获取到用户信息为" + user);
        return user;
    }

    public static List<UserVo> getUserListByDptCode(String dptCode, String  token){
        Map buss = MapUtil.builder().put("paramVar", dptCode).build();
        Map base = MapUtil.builder().put("usertoken", token).build();
        Map param = MapUtil.builder().put("buss", buss).put("base", base).build();
        HttpResponse response = HttpUtil.createPost(url+"/dpt/getUsreListByOrgCode").body(JSON.toJSONString(param), "application/json").execute();
        logger.info("调用查询部门接口返参：" + response.body());
        List<UserVo> dpt = null;
        if(StrUtil.isNotEmpty(response.body())){
            Map map = JSONObject.parseObject(response.body(), Map.class);
            dpt = JSONObject.parseArray(String.valueOf(map.get("buss")), UserVo.class);
        }
        return dpt;
    }
}
